import { Player } from "../player";
import {  MotionMode } from "../physics/player-physics";
import { Vector2 } from "../vector";
import { Action } from "./action-manager";
import "../ZzFX.micro";
export declare var zzfx: any;

// src/actions/magic-attack.ts  
/**
 * 魔法攻击动作类，实现Action接口
 * 
 * 该类负责处理玩家魔法攻击的完整流程，包括：
 * 1. 施法阶段（持续400ms）：播放施法动画、音效和粒子效果
 * 2. 发射阶段：创建魔法弹射物并播放发射音效
 * 3. 结束阶段：恢复玩家状态
 * 
 * @property name 动作名称
 * @property duration 动作总持续时间（800ms）
 * @property startTime 动作开始时间
 * @property isActive 动作是否激活
 * @private castingPhase 当前是否处于施法阶段
 */
export class MagicAttackAction implements Action {
    name = 'magic-attack';  
  duration = 3000; // 0.8秒  
  startTime = 0;  
  isActive = false;  
  private castingPhase = true;  
    
  onStart(player: Player) {  
    player.physics.mode_ = MotionMode.attacking;  
    this.castingPhase = true;  
      
    // 施法音效  
    zzfx(0.6, 0.2, 300, 0.1, 0.5, 3, 1, 0, 0.3);  
      
    // 施法粒子效果  
    player.engine.particles.emit({  
      count: 15,  
      direction_: new Vector2(2, 0),  
      lifetime: 300,  
      pos: player.body_.pos,  
    });  
  }  
    
  update(player: Player, elapsed: number) {  
    if (this.castingPhase && elapsed > 400) {  
      // 施法阶段结束，发射魔法弹  
      this.castingPhase = false;  
      this.fireMagicProjectile(player);  
    }  
      
    if (elapsed < 400) {  
      const progress = elapsed / 400;  
      player.animation_.rArmRot = -2.0 + Math.sin(progress * Math.PI * 4) * 0.3;  
      player.animation_.lArmRot = -2.0 + Math.cos(progress * Math.PI * 4) * 0.3;  
      player.animation_.headOffset = Math.sin(progress * Math.PI * 6) * 1;  
      player.animation_.scale_ = 1.0 + Math.sin(progress * Math.PI * 8) * 0.1;  
    } 
    else {  
      // 发射后恢复动画  
      const progress = (elapsed - 400) / 400;  
      player.animation_.rArmRot = -2.0 + progress * 1.0;  
      player.animation_.lArmRot = -2.0 + progress * 1.0;  
      player.animation_.scale_ = 1;  
    }  
  }  
    
  private fireMagicProjectile(player: Player) {  
    // 确定发射方向  
    const direction = new Vector2(player.physics.direction_ === "r" ? 1 : -1, 0);  
      
    // 发射位置（玩家前方）  
    const startPos = player.body_.pos.copy().add_(new Vector2(direction.x * 15, -10));  
      
    // 创建弹射物  
    player.engine.projectileManager.createProjectile(startPos, direction, 10);  
      
    // 发射音效  
    zzfx(0.8, 0.1, 400, 0.2, 0.3, 2, 1, 0, 0.2);  
  }  
    
  onEnd(player: Player) {  
    player.physics.mode_ = MotionMode.falling;  
    player.animation_.scale_ = 1;  
  }  
}